package jackson_demo1;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class jdemo1 {

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        User user = new User("杨通通", 32);
        //序列化
        String userStr = mapper.writeValueAsString(user);
        System.out.println(userStr);

        User user1 = mapper.readValue(userStr, User.class);
        System.out.println(user1);

        Map<String,Object> map = mapper.readValue(userStr, Map.class);
        System.out.println("map");
        System.out.println(map);


        List<User> list = Arrays.asList(user1,new User("成业",32));
        String listStr = mapper.writeValueAsString(list);
        System.out.println(listStr);
        List<User> parsedUsers = mapper.readValue(listStr, mapper.getTypeFactory().constructCollectionType(List.class, User.class));
        System.out.println(parsedUsers.get(0));

        List<User> users2 = mapper.readValue(listStr, new TypeReference<List<User>>() {
        });
        System.out.println(users2.get(0));
    }


}

@Data
@AllArgsConstructor
@NoArgsConstructor
class User{

    @JsonProperty("userName")
    private String name;
    private int age;

}
